package com.anbinh.doctor.fragment.clinic;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.AccountAdapter;
import com.anbinh.doctor.model.ItemMeu;
import com.anbinh.doctor.model.NewAvatar;
import com.anbinh.doctor.model.ResponseDoctor.Data.Doctors;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class FragmentDoctorDetailt extends Fragment {
	private static FragmentDoctorDetailt fragment = null;
	private View mView;
	List<ItemMeu> mList;
	private CircleProgressBar loadding;
	private ImageView avatar;
	private UserLogin userLogin;
	private TextView lblName;
	private Doctors currentDoctor;
	private TextView lblBrithday;

	public static FragmentDoctorDetailt newInstance() {
		if (fragment == null) {
			fragment = new FragmentDoctorDetailt();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);

		if (userLogin != null)
			lblName.setText(userLogin.getData().getUserInfo().getFull_name());

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_message, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		ListView mGridView = (ListView) mView.findViewById(R.id.listView1);
		View header = getActivity().getLayoutInflater().inflate(
				R.layout.header_assistiant, null);
		mGridView.addHeaderView(header);
		loadding = (CircleProgressBar) header.findViewById(R.id.loadding);

		lblName = (TextView) header.findViewById(R.id.lblName);
		lblBrithday = (TextView) header.findViewById(R.id.lblBrithday);
		lblBrithday.setText(FragmentDoctorList.newInstance()
				.getCurrentDoctors().getSpeciality());

		lblName.setText(FragmentDoctorList.newInstance().getCurrentDoctors()
				.getName());
		avatar = (ImageView) header.findViewById(R.id.avatar);
		avatar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Utils.pickFileImage(getActivity(), Utils.UP_IMAGE_DOCTOR);
			}
		});
		ImageLoader imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()
				.getApplicationContext()));
		currentDoctor = FragmentDoctorList.newInstance().getCurrentDoctors();
		Bitmap bmp = imageLoader.loadImageSync(currentDoctor.getAvatar());
		if (bmp == null) {

			imageLoader.displayImage(currentDoctor.getAvatar(), avatar,
					Utils.getOptions(getActivity()),
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingStarted(String imageUri, View view) {
						}

						@Override
						public void onLoadingFailed(String imageUri, View view,
								FailReason failReason) {
						}

						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
						}
					});
		} else {
			avatar.setImageBitmap(bmp);
		}

		mList = Utils.getMenu(getActivity(), R.menu.doctor_menu);
		AccountAdapter adapter = new AccountAdapter(getActivity(),
				R.layout.item_account, mList);
		mGridView.setAdapter(adapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 0)
					return;
				onClickItem(position - 1);
			}
		});
	}

	public void updateInfo(Intent data) {
		final UserLogin user = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);

		String token = user.getData().getToken();
		Uri selectedImage = data.getData();
		avatar.setImageURI(selectedImage);
		File file = new File(Utils.getRealPathFromURI(getActivity(),
				selectedImage));
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		ServerController.getInstance(getActivity()).requestUploadAvatarDoctor(
				token, file,
				FragmentDoctorList.newInstance().getCurrentDoctors().getId(),
				new OnRequest<NewAvatar>() {

					@Override
					public void onSuccess(NewAvatar data) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
						if (data.getResponsecode() >= 0) {

						}
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
						Utils.showMessage(getActivity(), message);
					}
				});
	}

	public void pickFile(Activity activity, int requestCode) {
		Intent intent = new Intent(Intent.ACTION_PICK);

		// TODO Auto-generated method stub
		// call android default gallery
		intent.setType("image/*");
		// ******** code for crop image
		activity.startActivityForResult(intent, requestCode);

	}

	public void onClickItem(int position) {
		int id = mList.get(position).getRegId();
		Bundle bundle = new Bundle();
		bundle.putInt(Utils.KEY, id);

		bundle.putInt(Utils.KEY_DOCTORID, currentDoctor.getId());
		bundle.putString(Utils.TITLE, mList.get(position).getTitle());
		if (id == R.id.lich_kham_doctor || id == R.id.lich_hen_doctor) {
			bundle.putBoolean(Utils.CUSTOMBAR, false);
			bundle.putInt(Utils.KEY_SELECT_MONTH, Utils.EXIST_MONTH);
		} else if (id == R.id.tin_nhan_doctor||id == R.id.benh_nhan_doctor) {
			bundle.putBoolean(Utils.CUSTOMBAR, false);
		} else {
			bundle.putBoolean(Utils.CUSTOMBAR, true);
		}

		Utils.getNewIntent(getActivity(), bundle);
	}

}
